#include <stdio.h>
#include <SDL/SDL.h>

int main(int argc, char *argv[])
{
    SDL_Surface *screen;	//This pointer will reference the backbuffer
    SDL_Surface *image;	//This pointer will reference our bitmap sprite

    SDL_Surface *up;
    SDL_Surface *down;
    SDL_Surface *left;
    SDL_Surface *right;

    //SDL_Surface *temp;	//This pointer will temporarily reference our bitmap sprite
    //SDL_Rect src, dest;	//These rectangles will describe the source and destination regions of our blit
    SDL_Rect helico;
    SDL_Event event;

    int done=1;
    int direction=0; //0:right 1:left 2:up 3:down

    //We must first initialize the SDL video component, and check for success
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        printf("Unable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }

    //Set the video mode to fullscreen 640x480 with 16bit colour and double-buffering
    screen = SDL_SetVideoMode(798, 601, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        printf("Unable to set video mode: %s\n", SDL_GetError());
        return 1;
    }

    //Load the bitmap into a temporary surface, and check for success
    image = SDL_LoadBMP("img/image.bmp");
    if (image == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    right = SDL_LoadBMP("img/helico.bmp");
    if (right == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    left = SDL_LoadBMP("img/left.bmp");
    if (left == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    up = SDL_LoadBMP("img/up.bmp");
    if (up == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    down = SDL_LoadBMP("img/down.bmp");
    if (down == NULL)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }

    // set the color of helico background (white color) as transparent
    SDL_SetColorKey( right, SDL_SRCCOLORKEY, SDL_MapRGB(right->format, 255, 255, 255) );
    SDL_SetColorKey( left, SDL_SRCCOLORKEY, SDL_MapRGB(left->format, 255, 255, 255) );
    SDL_SetColorKey( up, SDL_SRCCOLORKEY, SDL_MapRGB(up->format, 255, 255, 255) );
    SDL_SetColorKey( down, SDL_SRCCOLORKEY, SDL_MapRGB(down->format, 255, 255, 255) );


    helico.x=screen->w /2;
    helico.y=screen->h /2;
    helico.w=right->w;
    helico.h=right->h;

    while(done)
    {

        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = 0;
                break;

                // check for keypresses
            case SDL_KEYDOWN:
            {
                // exit if ESCAPE is pressed
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    done = 0;
                if(event.key.keysym.sym == SDLK_RIGHT)
                {
                    if((helico.x + helico.w) < image->w) //limite droite
                        helico.x +=10;
                    direction=0;
                }

                if(event.key.keysym.sym == SDLK_LEFT)
                {
                    if((helico.x) > 0) //limite gauche
                        helico.x -=10;
                    direction=1;
                }

                if(event.key.keysym.sym == SDLK_UP)
                {
                    if((helico.y) > 0) //limite UP
                        helico.y -=10;
                    direction=2;
                }

                if(event.key.keysym.sym == SDLK_DOWN)
                {
                    if((helico.y + helico.h) < image->h) //limite DOWN
                        helico.y +=10;
                    direction=3;
                }

                break;
            }

            } // end switch
        } // end of message processing



        //Blit the image to the backbuffer
        SDL_BlitSurface(image, NULL, screen,NULL);

        //Blit the helico to screen
        if(direction == 0)
            SDL_BlitSurface(right,NULL,screen,&helico);
        if(direction == 1)
            SDL_BlitSurface(left,NULL,screen,&helico);
        if(direction == 2)
            SDL_BlitSurface(up,NULL,screen,&helico);
        if(direction == 3)
            SDL_BlitSurface(down,NULL,screen,&helico);


        //clean screen &&
        //Flip the backbuffer to the primary Hardware Video Memory
        SDL_Flip(screen);

    }

    //Release the surface
    SDL_FreeSurface(image);

    //Return success!
    return 0;
}
